package org.example.controller;

import org.example.model.Award;
import org.example.model.User;
import org.example.service.AwardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/award")
public class AwardController {

    @Autowired
    private AwardService awardService;

    @PostMapping("/add")
    public Object add(@RequestBody Award award, HttpSession session){ // 插入时, 请求数据不带 id
        User user = (User) session.getAttribute("user");
        award.setSettingId(user.getSettingId());
        int n = awardService.add(award, user.getId());
        return award.getId();// 这里插入后，返回给前端自增主键id
    }

    @PostMapping("/update")
    public Object update(@RequestBody Award award){ // 修改是 id 修改
        int n = awardService.update(award);
        return null;
    }

    @GetMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id){ // @PathVariable 解析路径变量 , 这个注解里面的值绑定路径变量,
        // 如果里面没有值, 就绑定为变量名
        int n = awardService.delete(id);
        return null;
    }
}
